remove remaining shadows of cet (#971)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 6 Jan 2023 12:56:06 +0000 (05:56 -0700)
committerGitHub <noreply@github.com>
Fri, 6 Jan 2023 12:56:06 +0000 (05:56 -0700)
* wean garmin_gpi from global_opts.codec and cet.

* wean garmin from global_opts.codec and cet.

* remove cet

* don't include deprecated includes, no global codec.

* fix garmin includes

* fix up comment indicating garmin encoding issues.

* fiddle with encoding functions.

* change get_codec signature.

* trailling white space

16 files changed:
CMakeLists.txt
cet_util.cc [deleted file]
cet_util.h [deleted file]
defs.h
deprecated/cet_util.cc [new file with mode: 0644]
deprecated/cet_util.h [new file with mode: 0644]
garmin.cc
garmin_fs.cc
garmin_fs.h
garmin_gpi.cc
garmin_gpi.h
gdb.cc
igc.cc
main.cc
nmea.cc
util.cc

index 0c4448d50a589457356b42506f314ceb2b271aef..0d49215012e3cd055bb18727710cf141b8092055 100644 (file)
@@ -165,7 +165,6 @@ set(JEEPS
 
 # SUPPORT
 set(SUPPORT
-  cet_util.cc
   csv_util.cc
   fatal.cc
   filter_vecs.cc
@@ -203,7 +202,6 @@ endif()
 
 # HEADERS
 set(HEADERS
-  cet_util.h
   csv_util.h
   defs.h
   dg-100.h
diff --git a/cet_util.cc b/cet_util.cc
deleted file mode 100644 (file)
index b3451c6..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
-
-    Character encoding transformation - utilities
-
-    Copyright (C) 2005-2008 Olaf Klein, o.b.klein@gpsbabel.org
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-*/
-
-#include <QByteArray>            // for QByteArray
-#include <QDebug>                // for QDebug
-#include <QTextCodec>            // for QTextCodec
-
-#include "defs.h"
-#include "cet_util.h"
-#include "src/core/logging.h"    // for Fatal
-
-
-#define MYNAME "cet_util"
-
-/* gpsbabel additions */
-
-void
-cet_convert_deinit()
-{
-  global_opts.codec = nullptr;
-}
-
-void
-cet_convert_init(const QString& cs_name, const int force)
-{
-  if ((force != 0) || (global_opts.codec == nullptr)) {
-    cet_convert_deinit();
-    if (cs_name.isEmpty()) {   /* set default us-ascii */
-      global_opts.codec = QTextCodec::codecForName(CET_CHARSET_ASCII);
-    } else {
-      global_opts.codec = QTextCodec::codecForName(CSTR(cs_name));
-    }
-    if (!global_opts.codec) {
-      fatal(FatalMsg() << "Unsupported character set \"" << cs_name << ".");
-    }
-  }
-}
diff --git a/cet_util.h b/cet_util.h
deleted file mode 100644 (file)
index cc0e4f5..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
-
-    Character encoding transformation - utilities header
-
-    Copyright (C) 2005-2008 Olaf Klein, o.b.klein@gpsbabel.org
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-*/
-
-#ifndef CET_UTIL_H_INCLUDED_
-#define CET_UTIL_H_INCLUDED_
-
-#include <QString>
-#include "defs.h"
-
-
-/* gpsbabel extensions */
-
-void cet_convert_init(const QString& cs_name, int force);
-void cet_convert_deinit();
-
-#endif  // CET_UTIL_H_INCLUDED_
diff --git a/defs.h b/defs.h
index fe74f4f741b03adc884de08601a270c1f201946b..c56bb162d57aaac9eede1da6b2d634ab8a2fa6f2 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -48,8 +48,6 @@
 
 #define CSTR(qstr) ((qstr).toUtf8().constData())
 #define CSTRc(qstr) ((qstr).toLatin1().constData())
-#define STRFROMUNICODE(qstr) (global_opts.codec->fromUnicode(qstr).constData())
-#define STRTOUNICODE(cstr) (global_opts.codec->toUnicode(cstr))
 
 /*
  * Amazingly, this constant is not specified in the standard...
@@ -182,7 +180,6 @@ struct global_options {
   int smart_icons;
   int smart_names;
   inifile_t* inifile;
-  QTextCodec* codec;
 };
 
 extern global_options global_opts;
@@ -1064,6 +1061,7 @@ void gb_setbit(void* buf, uint32_t nr);
 void* gb_int2ptr(int i);
 int gb_ptr2int(const void* p);
 
+QTextCodec* get_codec(const QByteArray& cs_name);
 void list_codecs();
 void list_timezones();
 QString grapheme_truncate(const QString& input, unsigned int count);
diff --git a/deprecated/cet_util.cc b/deprecated/cet_util.cc
new file mode 100644 (file)
index 0000000..b3451c6
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+
+    Character encoding transformation - utilities
+
+    Copyright (C) 2005-2008 Olaf Klein, o.b.klein@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#include <QByteArray>            // for QByteArray
+#include <QDebug>                // for QDebug
+#include <QTextCodec>            // for QTextCodec
+
+#include "defs.h"
+#include "cet_util.h"
+#include "src/core/logging.h"    // for Fatal
+
+
+#define MYNAME "cet_util"
+
+/* gpsbabel additions */
+
+void
+cet_convert_deinit()
+{
+  global_opts.codec = nullptr;
+}
+
+void
+cet_convert_init(const QString& cs_name, const int force)
+{
+  if ((force != 0) || (global_opts.codec == nullptr)) {
+    cet_convert_deinit();
+    if (cs_name.isEmpty()) {   /* set default us-ascii */
+      global_opts.codec = QTextCodec::codecForName(CET_CHARSET_ASCII);
+    } else {
+      global_opts.codec = QTextCodec::codecForName(CSTR(cs_name));
+    }
+    if (!global_opts.codec) {
+      fatal(FatalMsg() << "Unsupported character set \"" << cs_name << ".");
+    }
+  }
+}
diff --git a/deprecated/cet_util.h b/deprecated/cet_util.h
new file mode 100644 (file)
index 0000000..cc0e4f5
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+
+    Character encoding transformation - utilities header
+
+    Copyright (C) 2005-2008 Olaf Klein, o.b.klein@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+#ifndef CET_UTIL_H_INCLUDED_
+#define CET_UTIL_H_INCLUDED_
+
+#include <QString>
+#include "defs.h"
+
+
+/* gpsbabel extensions */
+
+void cet_convert_init(const QString& cs_name, int force);
+void cet_convert_deinit();
+
+#endif  // CET_UTIL_H_INCLUDED_
index 14166c3eb130f81d2eb1ff50f1d305dd260c36d2..0b1ba3b8727d28caa18f0ebe7c4876252c46c399 100644 (file)
--- a/garmin.cc
+++ b/garmin.cc
 #include <QByteArray>            // for QByteArray
 #include <QChar>                 // for QChar
 #include <QString>               // for QString
+#include <QTextCodec>            // for QTextCodec
 #include <QVector>               // for QVector
 #include <Qt>                    // for CaseInsensitive
 #include <QtGlobal>              // for qPrintable, foreach
 
 #include "defs.h"
-#include "cet_util.h"            // for cet_convert_init, cet_cs_vec_utf8
 #include "format.h"              // for Format
+#include "formspec.h"            // for FormatSpecificDataList
 #include "garmin_device_xml.h"   // for gdx_get_info, gdx_info, gdx_file, gdx_jmp_buf
 #include "garmin_fs.h"           // for garmin_fs_garmin_after_read, garmin_fs_garmin_before_write
 #include "garmin_tables.h"       // for gt_find_icon_number_from_desc, PCX, gt_find_desc_from_icon_number
@@ -78,6 +79,7 @@ static char* baudopt = nullptr;
 static int baud = 0;
 static int categorybits;
 static int receiver_must_upper = 1;
+static QTextCodec* codec{nullptr};
 
 static Format* gpx_vec;
 
@@ -129,13 +131,17 @@ QVector<arglist_t> garmin_args = {
 
 static const char* d103_symbol_from_icon_number(unsigned int n);
 static int d103_icon_number_from_symbol(const QString& s);
+static void garmin_fs_garmin_after_read(GPS_PWay way, Waypoint* wpt, int protoid);
+static void garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, int protoid);
 
+static QByteArray str_from_unicode(const QString& qstr) {return codec->fromUnicode(qstr);}
+static QString str_to_unicode(const QByteArray& cstr) {return codec->toUnicode(cstr);}
 
 static void
 rw_init(const QString& fname)
 {
   receiver_must_upper = 1;
-  const char* receiver_charset = nullptr;
+  const char* receiver_charset = CET_CHARSET_ASCII;
 
   if (!mkshort_handle) {
     mkshort_handle = mkshort_new_handle();
@@ -335,15 +341,13 @@ rw_init(const QString& fname)
 
   /*
    * This used to mean something when we used cet, but these days this
-   * format either use implicit QString conversions (utf8) which is
-   * likely a bug, or we have hard coded QString::fromLatin1 or CSTRc.
-   * So all the above detection of receiver_charset is for naught.
-   * But perhaps we will use an appropriate codec based on receiver_charset
-   * someday.
+   * format either uses implicit QString conversions (utf8),
+   * or we have hard coded QString::fromLatin1, CSTRc, or CSTR.  These
+   * are likely bugs.
+   * However, this is still used for garmin_fs_garmin_after_read,
+   * garmin_fs_garmin_before_write.
    */
-  if (receiver_charset) {
-    cet_convert_init(receiver_charset, 1);
-  }
+  codec = get_codec(receiver_charset);
 }
 
 static void
@@ -1210,3 +1214,67 @@ d103_icon_number_from_symbol(const QString& s)
   }
   return 0;
 }
+
+static void
+garmin_fs_garmin_after_read(const GPS_PWay way, Waypoint* wpt, const int protoid)
+{
+  garmin_fs_t* gmsd = garmin_fs_alloc(protoid);
+  wpt->fs.FsChainAdd(gmsd);
+
+  /* nothing happens until gmsd is allocated some lines above */
+
+  /* !!! class needs protocol specific conversion !!! (ToDo)
+  garmin_fs_t::set_wpt_class(gmsd, way[i]->wpt_class);
+  */
+  /* flagged data fields */
+  garmin_fs_t::set_display(gmsd, gt_switch_display_mode_value(way->dspl, gps_waypt_type, 1));
+  if (way->category != 0) {
+    garmin_fs_t::set_category(gmsd, way->category);
+  }
+  if (way->dst < 1.0e25f) {
+    WAYPT_SET(wpt, proximity, way->dst);
+  }
+  if (way->temperature_populated) {
+    WAYPT_SET(wpt, temperature, way->temperature);
+  }
+  if (way->dpth < 1.0e25f) {
+    WAYPT_SET(wpt, depth, way->dpth);
+  }
+  /* will copy until a null character or the end of the fixed length way field is reached, whichever comes first. */
+  garmin_fs_t::set_cc(gmsd, str_to_unicode(QByteArray(way->cc, qstrnlen(way->cc, sizeof(way->cc)))));
+  garmin_fs_t::set_city(gmsd, str_to_unicode(QByteArray(way->city, qstrnlen(way->city, sizeof(way->city)))));
+  garmin_fs_t::set_state(gmsd, str_to_unicode(QByteArray(way->state, qstrnlen(way->state, sizeof(way->state)))));
+  garmin_fs_t::set_facility(gmsd, str_to_unicode(QByteArray(way->facility, qstrnlen(way->facility, sizeof(way->facility)))));
+  garmin_fs_t::set_cross_road(gmsd, str_to_unicode(QByteArray(way->cross_road, qstrnlen(way->cross_road, sizeof(way->cross_road)))));
+  garmin_fs_t::set_addr(gmsd, str_to_unicode(QByteArray(way->addr, qstrnlen(way->addr, sizeof(way->addr)))));
+}
+
+static void
+garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, const int protoid)
+{
+  garmin_fs_t* gmsd = garmin_fs_t::find(wpt);
+
+  (void)protoid; // unused for now.
+
+  if (gmsd == nullptr) {
+    return;
+  }
+
+  /* ToDo: protocol specific conversion of class
+  way[i]->wpt_class = garmin_fs_t::get_wpt_class(gmsd, way[i]->wpt_class);
+       */
+  way->dspl = gt_switch_display_mode_value(
+                garmin_fs_t::get_display(gmsd, way->dspl), gps_waypt_type, 0);
+  way->category = garmin_fs_t::get_category(gmsd, way->category);
+  way->dpth = WAYPT_GET(wpt, depth, way->dpth);
+  way->dst = WAYPT_GET(wpt, proximity, way->dpth);
+  way->temperature = WAYPT_GET(wpt, temperature, way->temperature);
+
+  /* destination may not be null terminated, but we will fill with nulls if necessary */
+  strncpy(way->cc, str_from_unicode(garmin_fs_t::get_cc(gmsd, nullptr)).constData(), sizeof(way->cc));
+  strncpy(way->city, str_from_unicode(garmin_fs_t::get_city(gmsd, nullptr)).constData(), sizeof(way->city));
+  strncpy(way->state, str_from_unicode(garmin_fs_t::get_state(gmsd, nullptr)).constData(), sizeof(way->state));
+  strncpy(way->facility, str_from_unicode(garmin_fs_t::get_facility(gmsd, nullptr)).constData(), sizeof(way->facility));
+  strncpy(way->cross_road, str_from_unicode(garmin_fs_t::get_cross_road(gmsd, nullptr)).constData(), sizeof(way->cross_road));
+  strncpy(way->addr, str_from_unicode(garmin_fs_t::get_addr(gmsd, nullptr)).constData(), sizeof(way->addr));
+}
index 62108942cf83b1b29472ac1bd9a5b5bd92b3926b..753bc5395d55a1f3a117711dca20fb50981c8e70 100644 (file)
 
  */
 
-#include <cassert>                   // for assert
 #include <cstdio>                    // for snprintf, sscanf
 #include <cstdlib>                   // for strtod
-#include <cstring>                   // for strncpy
 
-#include <QByteArray>                // for QByteArray
 #include <QString>                   // for QString, QStringLiteral
 #include <QXmlStreamWriter>          // for QXmlStreamWriter
 #include <Qt>                        // for CaseInsensitive
@@ -34,7 +31,6 @@
 #include "garmin_fs.h"
 #include "garmin_tables.h"           // for gt_switch_display_mode_value, gt_display_mode_symbol, gt_display_mode_symbol_and_comment, gt_display_mode_symbol_and_name
 #include "inifile.h"                 // for inifile_readstr
-#include "jeeps/gps.h"               // for gps_waypt_type
 
 
 #define MYNAME "garmin_fs"
@@ -320,67 +316,3 @@ garmin_fs_merge_category(const char* category_name, Waypoint* waypt)
   garmin_fs_t::set_category(gmsd, cat);
   return 1;
 }
-
-void
-garmin_fs_garmin_after_read(const GPS_PWay way, Waypoint* wpt, const int protoid)
-{
-  garmin_fs_t* gmsd = garmin_fs_alloc(protoid);
-  wpt->fs.FsChainAdd(gmsd);
-
-  /* nothing happens until gmsd is allocated some lines above */
-
-  /* !!! class needs protocol specific conversion !!! (ToDo)
-  garmin_fs_t::set_wpt_class(gmsd, way[i]->wpt_class);
-  */
-  /* flagged data fields */
-  garmin_fs_t::set_display(gmsd, gt_switch_display_mode_value(way->dspl, gps_waypt_type, 1));
-  if (way->category != 0) {
-    garmin_fs_t::set_category(gmsd, way->category);
-  }
-  if (way->dst < 1.0e25f) {
-    WAYPT_SET(wpt, proximity, way->dst);
-  }
-  if (way->temperature_populated) {
-    WAYPT_SET(wpt, temperature, way->temperature);
-  }
-  if (way->dpth < 1.0e25f) {
-    WAYPT_SET(wpt, depth, way->dpth);
-  }
-  // These use STRTOUNICODE which uses gobal_opts.codec.
-  garmin_fs_t::set_cc(gmsd, STRTOUNICODE(QByteArray(way->cc, sizeof(way->cc)).constData()));
-  garmin_fs_t::set_city(gmsd, STRTOUNICODE(QByteArray(way->city, sizeof(way->city)).constData()));
-  garmin_fs_t::set_state(gmsd, STRTOUNICODE(QByteArray(way->state, sizeof(way->state)).constData()));
-  garmin_fs_t::set_facility(gmsd, STRTOUNICODE(QByteArray(way->facility, sizeof(way->facility)).constData()));
-  garmin_fs_t::set_cross_road(gmsd, STRTOUNICODE(QByteArray(way->cross_road, sizeof(way->cross_road)).constData()));
-  garmin_fs_t::set_addr(gmsd, STRTOUNICODE(QByteArray(way->addr, sizeof(way->addr)).constData()));
-}
-
-void
-garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, const int protoid)
-{
-  garmin_fs_t* gmsd = garmin_fs_t::find(wpt);
-
-  (void)protoid; // unused for now.
-
-  if (gmsd == nullptr) {
-    return;
-  }
-
-  /* ToDo: protocol specific conversion of class
-  way[i]->wpt_class = garmin_fs_t::get_wpt_class(gmsd, way[i]->wpt_class);
-       */
-  way->dspl = gt_switch_display_mode_value(
-                garmin_fs_t::get_display(gmsd, way->dspl), gps_waypt_type, 0);
-  way->category = garmin_fs_t::get_category(gmsd, way->category);
-  way->dpth = WAYPT_GET(wpt, depth, way->dpth);
-  way->dst = WAYPT_GET(wpt, proximity, way->dpth);
-  way->temperature = WAYPT_GET(wpt, temperature, way->temperature);
-
-  // These use STRFROMUNICODE which uses gobal_opts.codec.
-  strncpy(way->cc, STRFROMUNICODE(garmin_fs_t::get_cc(gmsd, nullptr)), sizeof(way->cc));
-  strncpy(way->city, STRFROMUNICODE(garmin_fs_t::get_city(gmsd, nullptr)), sizeof(way->city));
-  strncpy(way->state, STRFROMUNICODE(garmin_fs_t::get_state(gmsd, nullptr)), sizeof(way->state));
-  strncpy(way->facility, STRFROMUNICODE(garmin_fs_t::get_facility(gmsd, nullptr)), sizeof(way->facility));
-  strncpy(way->cross_road, STRFROMUNICODE(garmin_fs_t::get_cross_road(gmsd, nullptr)), sizeof(way->cross_road));
-  strncpy(way->addr, STRFROMUNICODE(garmin_fs_t::get_addr(gmsd, nullptr)), sizeof(way->addr));
-}
index 13e8d4039e1d9bff7e3c0e8040a26481654585e8..1d9fd348e713a59271bcc25268e57312a14c709d 100644 (file)
@@ -32,7 +32,6 @@
 
 #include "defs.h"
 #include "formspec.h"               // for FsChainFind, kFsGmsd, FormatSpecificData
-#include "jeeps/gps.h"
 
 
 /* this order is used by most devices */
@@ -236,7 +235,4 @@ unsigned char garmin_fs_merge_category(const char* category_name, Waypoint* wayp
 
 #define GMSD_SECTION_CATEGORIES "Garmin Categories"
 
-void garmin_fs_garmin_after_read(GPS_PWay way, Waypoint* wpt, int protoid);
-void garmin_fs_garmin_before_write(const Waypoint* wpt, GPS_PWay way, int protoid);
-
 #endif
index aaa1e1fcb1a3e6372a461b6bd7bf7083c7e2e952..8c2d3c8e4b8e1d9e2a2e62ac55f1744140ce181e 100644 (file)
@@ -38,8 +38,7 @@
 #include <ctime>                   // for time, gmtime, time_t, tm
 #include <memory>                  // for unique_ptr
 
-#include "defs.h"                  // for Waypoint, fatal, STRFROMUNICODE, le_write32, le_write16, wp_flags, warning, bounds, KPH_TO_MPS, MPH_TO_MPS, WAYPT_HAS, gpsbabel_testmode, parse_speed, WAYPT_SET, MILES_TO_METERS, MPS_TO_KPH, MPS_TO_MPH, mkgmtime, mkshort, mkshort_del_handle, mkshort_new_...
-#include "cet_util.h"              // for cet_convert_init
+#include "defs.h"                  // for Waypoint, fatal, le_write32, le_write16, wp_flags, warning, bounds, KPH_TO_MPS, MPH_TO_MPS, WAYPT_HAS, gpsbabel_testmode, parse_speed, WAYPT_SET, MILES_TO_METERS, MPS_TO_KPH, MPS_TO_MPH, mkgmtime, mkshort, mkshort_del_handle, mkshort_new_...
 #include "formspec.h"              // for FormatSpecificDataList
 #include "garmin_fs.h"             // for garmin_fs_t, garmin_fs_alloc
 #include "gbfile.h"                // for gbfputint32, gbfgetint32, gbfgetint16, gbfputint16, gbfgetc, gbfputc, gbfread, gbftell, gbfwrite, gbfseek, gbfclose, gbfopen_le, gbfgetuint16, gbsize_t, gbfile
@@ -149,7 +148,7 @@ GarminGPIFormat::gpi_read_string(const char* field) const
     }
   }
 
-  QString result = STRTOUNICODE(string).trimmed();
+  QString result = str_to_unicode(string).trimmed();
   if (GPI_DBG) {
     warning("%s: \"%s\"\n", field, result.isNull() ? "<NULL>" : qPrintable(result));
   }
@@ -560,15 +559,15 @@ GarminGPIFormat::read_tag(const char* caller, const int tag, Waypoint* wpt)
 *******************************************************************************/
 
 void
-GarminGPIFormat::write_string(const char* str, const char long_format) const
+GarminGPIFormat::write_string(const QByteArray& str, const char long_format) const
 {
-  int len = strlen(str);
+  int len = str.size();
   if (long_format) {
     gbfputint32(len + 4, fout);
     gbfwrite("EN", 1, 2, fout);
   }
   gbfputint16(len, fout);
-  gbfwrite(str, 1, len, fout);
+  gbfwrite(str.constData(), 1, len, fout);
 }
 
 bool
@@ -707,7 +706,7 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const
 
     res += 12;    /* tag/sz/sub-sz */
     res += 19;    /* poi fixed size */
-    res += strlen(STRFROMUNICODE(wpt->shortname));
+    res += str_from_unicode(wpt->shortname).size();
     if (! opt_hide_bitmap) {
       res += 10;  /* tag(4) */
     }
@@ -764,33 +763,33 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const
     if (!str.isEmpty()) {
       dt->addr = str;
       dt->mask |= GPI_ADDR_ADDR;
-      dt->sz += (8 + strlen(STRFROMUNICODE(dt->addr)));
+      dt->sz += (8 + str_from_unicode(dt->addr).size());
     }
 
     if ((gmsd = garmin_fs_t::find(wpt))) {
       if ((dt->mask == 0) && !(dt->addr = garmin_fs_t::get_addr(gmsd, nullptr)).isEmpty()) {
         dt->mask |= GPI_ADDR_ADDR;
-        dt->sz += (8 + strlen(STRFROMUNICODE(dt->addr)));
+        dt->sz += (8 + str_from_unicode(dt->addr).size());
       }
       if (!(dt->city = garmin_fs_t::get_city(gmsd, nullptr)).isEmpty()) {
         dt->mask |= GPI_ADDR_CITY;
-        dt->sz += (8 + strlen(STRFROMUNICODE(dt->city)));
+        dt->sz += (8 + str_from_unicode(dt->city).size());
       }
       if (!(dt->country = garmin_fs_t::get_country(gmsd, nullptr)).isEmpty()) {
         dt->mask |= GPI_ADDR_COUNTRY;
-        dt->sz += (8 + strlen(STRFROMUNICODE(dt->country)));
+        dt->sz += (8 + str_from_unicode(dt->country).size());
       }
       if (!(dt->state = garmin_fs_t::get_state(gmsd, nullptr)).isEmpty()) {
         dt->mask |= GPI_ADDR_STATE;
-        dt->sz += (8 + strlen(STRFROMUNICODE(dt->state)));
+        dt->sz += (8 + str_from_unicode(dt->state).size());
       }
       if (!(dt->postal_code = garmin_fs_t::get_postal_code(gmsd, nullptr)).isEmpty()) {
         dt->mask |= GPI_ADDR_POSTAL_CODE;
-        dt->sz += (2 + strlen(STRFROMUNICODE(dt->postal_code)));  /* short form */
+        dt->sz += (2 + str_from_unicode(dt->postal_code).size());  /* short form */
       }
 
       if (!(dt->phone_nr = garmin_fs_t::get_phone_nr(gmsd, nullptr)).isEmpty()) {
-        res += (12 + 4 +  strlen(STRFROMUNICODE(dt->phone_nr)));
+        res += (12 + 4 +  str_from_unicode(dt->phone_nr).size());
       }
     }
     if (dt->mask) {
@@ -806,7 +805,7 @@ GarminGPIFormat::wdata_compute_size(writer_data_t* data) const
     }
 //    if (str && (strcmp(str, wpt->shortname) == 0)) str = NULL;
     if (!str.isEmpty()) {
-      res += (12 + 4 + strlen(STRFROMUNICODE(str)));
+      res += (12 + 4 + str_from_unicode(str).size());
     }
   }
 
@@ -864,18 +863,18 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const
     }
 
     gbfputint32(0x80002, fout);
-    int s0 = s1 = 19 + strlen(STRFROMUNICODE(wpt->shortname));
+    int s0 = s1 = 19 + str_from_unicode(wpt->shortname).size();
     if (! opt_hide_bitmap) {
       s0 += 10;  /* tag(4) */
     }
     if (!str.isEmpty()) {
-      s0 += (12 + 4 + strlen(STRFROMUNICODE(str)));  /* descr */
+      s0 += (12 + 4 + str_from_unicode(str).size());  /* descr */
     }
     if (dt->sz) {
       s0 += (12 + dt->sz);  /* address part */
     }
     if (!dt->phone_nr.isEmpty()) {
-      s0 += (12 + 4 + strlen(STRFROMUNICODE(dt->phone_nr)));
+      s0 += (12 + 4 + str_from_unicode(dt->phone_nr).size());
     }
     if (dt->alerts) {
       s0 += 20;  /* tag(3) */
@@ -890,7 +889,7 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const
     gbfputint16(1, fout);  /* ? always 1 ? */
     gbfputc(alerts, fout);  /* seems to be 1 when extra options present */
 
-    write_string(STRFROMUNICODE(wpt->shortname), 1);
+    write_string(str_from_unicode(wpt->shortname), 1);
 
     if (dt->alerts) {
       char flag = 0;
@@ -926,8 +925,8 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const
 
     if (!str.isEmpty()) {
       gbfputint32(0xa, fout);
-      gbfputint32(strlen(STRFROMUNICODE(str)) + 8, fout);  /* string + string header */
-      write_string(STRFROMUNICODE(str), 1);
+      gbfputint32(str_from_unicode(str).size() + 8, fout);  /* string + string header */
+      write_string(str_from_unicode(str), 1);
     }
 
     if (dt->sz) {          /* gpi address */
@@ -936,28 +935,28 @@ GarminGPIFormat::wdata_write(const writer_data_t* data) const
       gbfputint32(0x2, fout);      /* ? always 2 ? */
       gbfputint16(dt->mask, fout);
       if (dt->mask & GPI_ADDR_CITY) {
-        write_string(STRFROMUNICODE(dt->city), 1);
+        write_string(str_from_unicode(dt->city), 1);
       }
       if (dt->mask & GPI_ADDR_COUNTRY) {
-        write_string(STRFROMUNICODE(dt->country), 1);
+        write_string(str_from_unicode(dt->country), 1);
       }
       if (dt->mask & GPI_ADDR_STATE) {
-        write_string(STRFROMUNICODE(dt->state), 1);
+        write_string(str_from_unicode(dt->state), 1);
       }
       if (dt->mask & GPI_ADDR_POSTAL_CODE) {
-        write_string(STRFROMUNICODE(dt->postal_code), 0);
+        write_string(str_from_unicode(dt->postal_code), 0);
       }
       if (dt->mask & GPI_ADDR_ADDR) {
-        write_string(STRFROMUNICODE(dt->addr), 1);
+        write_string(str_from_unicode(dt->addr), 1);
       }
     }
 
     if (!dt->phone_nr.isEmpty()) {
       gbfputint32(0x8000c, fout);
-      gbfputint32(strlen(STRFROMUNICODE(dt->phone_nr)) + 2 + 2, fout);
+      gbfputint32(str_from_unicode(dt->phone_nr).size() + 2 + 2, fout);
       gbfputint32(0x2, fout);      /* ? always 2 ? */
       gbfputint16(1, fout);      /* mask */
-      write_string(STRFROMUNICODE(dt->phone_nr), 0);
+      write_string(str_from_unicode(dt->phone_nr), 0);
     }
   }
 
@@ -982,7 +981,7 @@ GarminGPIFormat::write_category(const char* /*unused*/, const unsigned char* ima
 {
   int sz = wdata_compute_size(wdata);
   sz += 8;  /* string header */
-  sz += strlen(STRFROMUNICODE(QString::fromUtf8(opt_cat)));
+  sz += str_from_unicode(QString::fromUtf8(opt_cat)).size();
 
   gbfputint32(0x80009, fout);
   if ((! opt_hide_bitmap) && image_sz) {
@@ -991,7 +990,7 @@ GarminGPIFormat::write_category(const char* /*unused*/, const unsigned char* ima
     gbfputint32(sz, fout);
   }
   gbfputint32(sz, fout);
-  write_string(STRFROMUNICODE(QString::fromUtf8(opt_cat)), 1);
+  write_string(str_from_unicode(QString::fromUtf8(opt_cat)), 1);
 
   wdata_write(wdata);
 
@@ -1229,10 +1228,10 @@ GarminGPIFormat::rd_init(const QString& fname)
   read_header();
 
   if ((codepage >= 1250) && (codepage <= 1257)) {
-    QString qCodecName = QStringLiteral("windows-%1").arg(codepage);
-    cet_convert_init(CSTR(qCodecName), 1);
+    QByteArray qCodecName = "windows-" + QByteArray::number(codepage);
+    codec = get_codec(qCodecName);
   } else if (codepage == 65001) {
-    cet_convert_init("utf8", 1);
+    codec = get_codec("utf8");
   } else {
     fatal(MYNAME ": Unsupported code page (%d). File is likely encrypted.\n", codepage);
   }
@@ -1288,7 +1287,7 @@ GarminGPIFormat::wr_init(const QString& fname)
     fatal(MYNAME ": Valid values are windows-1250 to windows-1257 and utf8.\n");
   }
 
-  cet_convert_init(opt_writecodec,1);
+  codec = get_codec(opt_writecodec);
 
   units = tolower(opt_units[0]);
   if ((units != 'm') && (units != 's')) {
index ee9c32f4858649eed034a7cdf22394d65e8f749b..a339d0e8b6fd1a868384d5c7ccc07b4aac88149c 100644 (file)
@@ -49,6 +49,7 @@
 #include <QByteArray>   // for QByteArray
 #include <QList>        // for QList
 #include <QString>      // for QString
+#include <QTextCodec>   // for QTextCodec
 #include <QVector>      // for QVector
 
 #include <cstdint>      // for int32_t, int16_t, uint16_t
@@ -297,7 +298,7 @@ private:
   void read_poi_list(int sz);
   void read_poi_group(int sz, int tag);
   int read_tag(const char* caller, int tag, Waypoint* wpt);
-  void write_string(const char* str, char long_format) const;
+  void write_string(const QByteArray& str, char long_format) const;
   static bool compare_wpt_cb(const Waypoint* a, const Waypoint* b);
   static char compare_strings(const QString& s1, const QString& s2);
   static writer_data_t* wdata_alloc();
@@ -310,6 +311,8 @@ private:
   void write_header() const;
   void enum_waypt_cb(const Waypoint* ref) const;
   static void load_bitmap_from_file(const char* fname, const unsigned char** data, int* data_sz);
+  QByteArray str_from_unicode(const QString& qstr) const {return codec->fromUnicode(qstr);}
+  QString str_to_unicode(const QByteArray& cstr) const {return codec->toUnicode(cstr);}
 
   /* Data Members */
 
@@ -387,6 +390,7 @@ private:
   short_handle short_h{};
   char units{};
   time_t gpi_timestamp = 0;
+  QTextCodec* codec{nullptr};
 };
 
 #endif // GARMIN_GPI_H_INCLUDED_
diff --git a/gdb.cc b/gdb.cc
index de388f4b5bc69698ea222891e9162c544a884932..e205c2d55c6076089639ea6830bbf05d3f1bd34d 100644 (file)
--- a/gdb.cc
+++ b/gdb.cc
@@ -40,7 +40,6 @@
 #include <iterator>                // for next
 
 #include "defs.h"                  // for Waypoint, warning, fatal, route_head, UrlLink, bounds, mkshort, UrlList, unknown_alt, wp_flags, xfree, waypt_add_to_bounds, waypt_init_bounds, mkshort_del_handle, route_add_wpt, route_disp_all, waypt_bounds_valid, xmalloc, WAYPT_GET, WAYPT_SET, gb_color
-#include "cet_util.h"              // for cet_convert_init
 #include "formspec.h"              // for FormatSpecificDataList
 #include "garmin_fs.h"             // for garmin_fs_t, garmin_ilink_t, garmin_fs_alloc
 #include "garmin_tables.h"         // for gt_waypt_class_map_point, gt_color_index_by_rgb, gt_color_value, gt_waypt_classes_e, gt_find_desc_from_icon_number, gt_find_icon_number_from_desc, gt_gdb_display_mode_symbol, gt_get_icao_country, gt_waypt_class_user_waypoint, GDB, gt_display_mode_symbol
@@ -970,10 +969,6 @@ GdbFormat::rd_init(const QString& fname)
   fin = gbfopen_le(fname, "rb", MYNAME);
   ftmp = gbfopen_le(nullptr, "wb", MYNAME);
   read_file_header();
-  /* VERSION DEPENDENT CODE */
-  if (gdb_ver >= GDB_VER_UTF8) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
 
   wayptq_in.clear();
   wayptq_in_hidden.clear();
@@ -1734,10 +1729,6 @@ GdbFormat::wr_init(const QString& fname)
     gdb_category = strtol(gdb_opt_bitcategory, nullptr, 0);
   }
 
-  if (gdb_ver >= GDB_VER_UTF8) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
-
   wayptq_out.clear();
   short_h = nullptr;
 
diff --git a/igc.cc b/igc.cc
index 3ab9fef0abe559a8960cfb6089e0cc570f631a0f..44a187405761f8ef43bb9920185ed4db126497e4 100644 (file)
--- a/igc.cc
+++ b/igc.cc
@@ -37,7 +37,6 @@
 #include <QtGlobal>                  // for foreach, qPrintable
 
 #include "defs.h"
-#include "cet_util.h"                // for cet_convert_init
 #include "gbfile.h"                  // for gbfprintf, gbfclose, gbfopen, gbfputs, gbfgetstr, gbfile
 #include "src/core/datetime.h"       // for DateTime
 
@@ -109,9 +108,6 @@ static igc_rec_type_t get_record(char** rec)
   char* c;
 retry:
   *rec = c = gbfgetstr(file_in);
-  if ((lineno++ == 0) && file_in->unicode) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
   if (c == nullptr) {
     return rec_none;
   }
diff --git a/main.cc b/main.cc
index 52bcb902811cde425f5cc923f33e4369568a4ff1..299406f825b659a7753a6392ab17584fe8f40e43 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -44,7 +44,6 @@
 #endif
 
 #include "defs.h"
-#include "cet_util.h"                 // for cet_convert_init, cet_convert_deinit
 #include "csv_util.h"                 // for csv_linesplit
 #include "filter.h"                   // for Filter
 #include "filter_vecs.h"              // for FilterVecs
@@ -343,15 +342,11 @@ run(const char* prog_name)
         global_opts.masked_objective |= WPTDATAMASK;
       }
 
-      cet_convert_init(ivecs->get_encode(), ivecs->get_fixed_encode());        /* init by module vec */
-
       start_session(ivecs->get_name(), fname);
       ivecs->rd_init(fname);
       ivecs->read();
       ivecs->rd_deinit();
 
-      cet_convert_deinit();
-
       did_something = true;
       break;
     case 'F':
@@ -366,13 +361,10 @@ run(const char* prog_name)
           global_opts.masked_objective |= WPTDATAMASK;
         }
 
-        cet_convert_init(ovecs->get_encode(), ovecs->get_fixed_encode());
-
         ovecs->wr_init(ofname);
         ovecs->write();
         ovecs->wr_deinit();
 
-        cet_convert_deinit();
       }
       break;
     case 's':
@@ -543,33 +535,25 @@ run(const char* prog_name)
     /* reinitialize xcsv in case two formats that use xcsv were given */
     (void) Vecs::Instance().find_vec(ivecs->get_argstring());
 
-    cet_convert_init(ivecs->get_encode(), 1);
-
     start_session(ivecs->get_name(), qargs.at(0));
     ivecs->rd_init(qargs.at(0));
     ivecs->read();
     ivecs->rd_deinit();
 
-    cet_convert_deinit();
-
     if (qargs.size() == 2 && ovecs) {
       /* reinitialize xcsv in case two formats that use xcsv were given */
       (void) Vecs::Instance().find_vec(ovecs->get_argstring());
 
-      cet_convert_init(ovecs->get_encode(), 1);
-
       ovecs->wr_init(qargs.at(1));
       ovecs->write();
       ovecs->wr_deinit();
 
-      cet_convert_deinit();
     }
   } else if (!qargs.isEmpty()) {
     usage(prog_name,0);
     return 0;
   }
   if (ovecs == nullptr) {
-    cet_convert_init(CET_CHARSET_ASCII, 1);
     auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void {
       fbOutput.waypt_disp(wpt);
     };
diff --git a/nmea.cc b/nmea.cc
index 431e505bf8521bab711e454c34e91ae31712233a..fc63c610afafa45536e90e091bfa0702240248f1 100644 (file)
--- a/nmea.cc
+++ b/nmea.cc
@@ -42,7 +42,6 @@
 
 #include "defs.h"
 #include "nmea.h"
-#include "cet_util.h"              // for cet_convert_init
 #include "gbfile.h"                // for gbfprintf, gbfflush, gbfclose, gbfopen, gbfgetstr, gbfile
 #include "gbser.h"                 // for gbser_set_speed, gbser_flush, gbser_read_line, gbser_deinit, gbser_init, gbser_write
 #include "jeeps/gpsmath.h"         // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M
@@ -989,10 +988,6 @@ NmeaFormat::read()
   while ((ibuf = gbfgetstr(file_in))) {
     line++;
 
-    if ((line == 0) & file_in->unicode) {
-      cet_convert_init(CET_CHARSET_UTF8, 1);
-    }
-
     if ((line == 0) && (case_ignore_strncmp(ibuf, "@SonyGPS/ver", 12) == 0)) {
       /* special hack for Sony GPS-CS1 files:
          they are fully (?) nmea compatible, but come with a header line like
diff --git a/util.cc b/util.cc
index 643c86244be3b4db6d8533b0d3ba462c9cf26228..e7d0e67672e2a45c78c65b2e6a344aade41b35aa 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -1346,6 +1346,15 @@ int gb_ptr2int(const void* p)
   return x.i;
 }
 
+QTextCodec* get_codec(const QByteArray& cs_name)
+{
+  QTextCodec* codec = QTextCodec::codecForName(cs_name);
+  if (codec == nullptr) {
+    fatal(FatalMsg().nospace() << "Unsupported character set " << cs_name << ".");
+  }
+  return codec;
+}
+
 void
 list_codecs()
 {